import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
  uinfo: JSON.parse(localStorage.getItem('uinfo')) || {}
}
const getters = {

}
const mutations = {
  SET_USERINFO (state, uinfo) {
    state.uinfo = uinfo
    localStorage.setItem('uinfo', JSON.stringify(uinfo))
  },
  DELETE_USERINFO (state) {
    window.localStorage.removeItem('uinfo')
    window.localStorage.removeItem('admin_jwt_token')
  },
  SET_TOKEN (state, data) {
    state.token = data
    window.localStorage.setItem('admin_jwt_token', state.token)
  }
}
const actions = {
  setUserInfo ({ commit }, data) {
    commit('SET_USERINFO', data)
  },
  deleteuserinfo ({ commit }) {
    commit('DELETE_USERINFO')
  },
  setToken ({ commit }, data) {
    commit('SET_TOKEN', data)
  }
}

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions
})
